// pages/my-lotto/info/info.js
var api = require("../../../utils/api.js");
var util = require("../../../utils/util.js");
var match = require("../../../utils/match.js")
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    luckInfo: {},
    showJoinView: false,
    showWinView: false,
    receiveInfo: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getInfo(options.id);
    // 测试跳转成功
    // util.toLuckSuccess();
  },
  //获取信息
  getInfo: function(id) {
    api.luckInfo(id, res => {
      this.setData({
        luckInfo: res,
        showWinView: res.is_win != 3 && res.is_join == 1 && !res.is_reveice,
      });
    }, res => {
      wx.showToast({
        title: res,
        icon:'none'
      })
    })
  },
  //参加按钮方法
  join: function(e) {
    this.setData({
      showJoinView: true,
    })
  },
  //隐藏参加界面
  hideJoinView: function(e) {
    this.setData({
      showJoinView: false,
    })
  },

  intercept: function(e) {

  },
  //参加抽奖
  joinLuck: function(e) {
    this.hideJoinView();
    wx.showLoading({
      title: '',
    });
    api.luckJoin(this.data.luckInfo.id, '', res => {
      wx.hideLoading();
      this.setData({
        ['luckInfo.is_join']: 1,
        ['luckInfo.ceil_people']:--this.data.luckInfo.ceil_people,
      })
      if(this.data.luckInfo.ceil_people <=0){
        this.getInfo(this.data.luckInfo.id); 
      }
    }, err => {
      wx.hideLoading();
      wx.showToast({
        title: err,
        icon: 'none'
      })
    });
  },
  //隐藏win界面
  hideWinView: function(e) {
    this.setData({
      showWinView: false,
    })
  },
  //领取
  exchange: function(e) {
    this.hideWinView();
  },
  //立即领取
  receive: function(e) {
    if (match.isMobile(this.data.receiveInfo.phone)) {
      wx.showToast({
        title: '请输入正确手机号',
        icon: 'none'
      })
      return;
    }
    if (this.data.receiveInfo.real_name && this.data.receiveInfo.real_name != "" && this.data.receiveInfo.address && this.data.receiveInfo.address != "") {
      api.writeLuck(this.data.luckInfo.join_id, this.data.receiveInfo, res => {
        this.setData({
          ['luckInfo.is_receive']: 1,
        })
        util.toLuckSuccess();
      }, err => {
        wx.showToast({
          title: err,
          icon: 'none'
        })
      })
    } else {
      wx.showToast({
        title: '请填写收货信息',
        icon: 'none'
      })
    }
  },

  //填写信息
  inputInfo: function(e) {
    var info = this.data.receiveInfo;
    info[e.currentTarget.dataset.type] = e.detail.value;
    // console.log(info);
    this.setData({
      receiveInfo: info,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return app.shareSeekHelp();
  }
})